<?php
namespace Wechat\Controller;
use Think\Page;
class VipController extends WechatController {
	
	/**
	 * 初始化设置主要用于分享
	 * @date: 2015-2-26
	 * @author: 明
	 * @return:
	 */
	public function _initialize(){
		parent::_initialize();
		$userconfiginfo = session('userconfiginfo');
		$signPackage = $this->getSignPackage();
		$this->assign('userconfiginfo',$userconfiginfo);
		$this->assign('signpackage',$signPackage);
			
	}
	
	/**
	 * 个人中心
	 * @date: 2015-11-6
	 * @author: 陈雄华
	 * @return:
	 */
	public function index(){
		$groupLogic = D('FightgroupGroup', 'Logic');
		$orderLogic = D('FightgroupOrder', 'Logic');
		$grouplogLogic = D('FightgroupGrouplog', 'Logic');
		//用户昵称
		$userinfo = session('userinfo');
		$openid = $userinfo['openid'];
		$nickname = $userinfo['nickname'];
		//计算成功开团个数
		//查看该人为团长时的团id
		$grouplogList = $grouplogLogic->where(array('openid' => $openid,'isleader' => 1,'uid' => WID)) -> field('groupid') -> select();
		$count = 0;//初始化成功开团个数
		foreach($grouplogList as $key => $value){
			//查询团状态
			$group = $groupLogic -> where(array('id' => $value['groupid'])) -> field('status') -> find();
			if($group['status'] == 4){//4表示团已完成，完成状态，团数加1
				$count++;
			}
		}
		
		//计算花费金额
		//对应openid所有订单处于完成状态的总金额
		$orderList = $orderLogic -> where(array('openid' => $openid, 'status' => 4)) -> field('totalprice') -> select();
		$totalprice = 0;
		foreach($orderList as $key => $value){
			$totalprice += $value['totalprice'];
		}
		
		$this->assign('totalprice', $totalprice);
		$this->assign('count', $count);
		$this->assign('userinfo', $userinfo);
		$this->display();
	}
	
	/**
	 * 收货地址列表
	 * @date: 2015-11-6
	 * @author: 陈雄华
	 * @return:
	 */
	public function addresslist(){
		$addressLogic = D('FightgroupAddress', 'Logic');
		//用户信息
		$userinfo = session('userinfo');
		$openid = $userinfo['openid'];
		
		//默认地址
		$address = $addressLogic -> where(array('openid' => $openid, 'isdefault' => 1, 'ishidden' => 0, 'uid' => WID)) -> find();
		$this -> assign('address', $address);
		
		$count= $addressLogic -> where(array('openid' => $openid, 'isdefault' => 0, 'uid' => WID)) -> count();
		$page =new Page($count, 10);
		$totalPages = $page -> totalPages;
		//该人其他地址
		$addressList = $addressLogic -> where(array('openid' => $openid, 'isdefault' => 0, 'ishidden' => 0, 'uid' => WID)) -> limit($page -> firstRow, $page -> listRows) -> select();
		if(IS_POST){
			if($addressList){
				$this -> ajaxReturn(array('ret' => 1, 'msg' => $addressList));
			}else{
				$this -> ajaxReturn(array('ret' => 0));
			}
		}
		
		$this -> display();
	}
	
	/**
	 * 收货地址删除
	 * @date: 2015-11-6
	 * @author: 陈雄华
	 * @return:
	 */
	public function addressdelete(){
		$addressLogic = D('FightgroupAddress', 'Logic');
		$id = I('get.id', 0);
		$res = $addressLogic -> where(array('id' => $id)) -> save(array('ishidden' => 1));
		if($res){
			redirect(U('Vip/addresslist'));
		}else{
			$this -> error('删除失败');
		}
	}
	
	/**
	 * 收货地址编辑、添加页面显示
	 * @date: 2015-11-6
	 * @author: 陈雄华
	 * @return:
	 */
	public function addressedit(){
		$addressLogic = D('FightgroupAddress', 'Logic');
		if(IS_GET){
			$id = I('get.id', 0);
			$address = $addressLogic -> where(array('id' => $id)) -> find();
			$this -> assign('address', $address);
		}
		$this -> display();
	}
	
	
	/**
	 * 收货地址编辑、添加操作
	 * @date: 2015-11-6
	 * @author: 陈雄华
	 * @return:
	 */
	public function addresseditupdate(){
		$addressLogic = D('FightgroupAddress', 'Logic');
		
		//用户信息
		$userinfo = session('userinfo');
		$openid = $userinfo['openid'];
		$nickname = $userinfo['nickname'];
		
		$id = I('post.id');
		$linktel = I('post.linktel');
		$isdefault = I('post.isdefault');
		//判断电话号码格式是否正确，错误则返回提示信息
		if(preg_match("/1[3458]{1}\d{9}$/", $linktel)){
			$linktel = $linktel;
		}else{
			$this->ajaxReturn ( array (
					'ret' => 2,
					'msg'=>'手机号码格式不正确',
			) );
		}
		
		//post过来的数据信息
		$where = array(
				'uid' => WID,
				'openid' => $openid,
				'nickname' => $nickname,
				'linkman' => I('post.linkman'),
				'linktel' => $linktel,
				'area' => I('post.area'),
				'address' => I('post.address'),
				'createtime' => time(),
				'isdefault' => $isdefault,
		);
		//$id为空，添加数据。反之修改操作
		if(empty($id)){
			if($isdefault == 1){
				//查找是否已经有了默认地址
				$address = $addressLogic -> where(array('openid' => $openid, 'isdefault' => 1, 'uid' => WID)) -> field('id') -> find();
				if($address['id'] == ''){
					$res = $addressLogic -> add($where);
				}else{
					$addressLogic -> where(array('id' => $address['id'])) -> save(array('isdefault' => 0));
					$res = $addressLogic -> add($where);
				}				
			}else{
				$res = $addressLogic -> add($where);
			}
			
			//判断操作是否成功
			if($res){
				$this -> ajaxReturn(array('ret' => 1, 'msg' => '添加成功'));
			}else{
				$this -> ajaxReturn(array('ret' => 2, 'msg' => '添加失败'));
			}
		}else{
			if($isdefault == 1){
				//查找是否已经有了默认地址
				$address = $addressLogic -> where(array('openid' => $openid, 'isdefault' => 1, 'uid' => WID)) -> field('id') -> find();
				if($address['id'] == ''){
					$res = $addressLogic -> where(array('id' => $id)) -> save($where);
				}else{
					$addressLogic -> where(array('id' => $address['id'])) -> save(array('isdefault' => 0));
					$res = $addressLogic -> where(array('id' => $id)) -> save($where);
				}
			}else{
				$res = $addressLogic -> where(array('id' => $id)) -> save($where);
			}
			
			
			if($res){
				$this -> ajaxReturn(array('ret' => 1, 'msg' => '修改成功'));
			}else{
				$this -> ajaxReturn(array('ret' => 2, 'msg' => '修改失败'));
			}
		}
		
	}
	
	/**
	 * 我的成团列表
	 * @date: 2015-11-10
	 * @author: 陈雄华
	 * @return:
	 */
	public function mygroup(){
		$grouplogLogic = D('FightgroupGrouplog', 'Logic');
		$groupLogic = D('FightgroupGroup', 'Logic');
		$orderLogic = D('FightgroupOrder', 'Logic');
		$productLogic = D('FightgroupProduct', 'Logic');
		
		if(IS_POST){
			//用户信息
			$userinfo = session('userinfo');
			$openid = $userinfo['openid'];
			$nickname = $userinfo['nickname'];
// 			$this->ajaxReturn(array('ret'=>1,'msg'=>'hello'));
			$where = array(
					'uid' => WID,
					'openid' => $openid,
					'ishidden' => 0,
			);
			$count= $grouplogLogic -> where($where) -> count();
			$page =new Page($count, 10);
			
			$grouplogList = $grouplogLogic -> where($where) -> order('createtime desc') -> limit($page -> firstRow, $page -> listRows) -> select();
			//判断团是否过期
			$time = time();
			foreach($grouplogList as $key => $value){
				$group = $groupLogic -> where(array('id' => $value['groupid'])) ->find();
				if($group['status'] == 1){
					if($time > $value['period'] && $value['period'] != 0){
						$orderLogic -> where(array('groupid' => $value['groupid'])) -> save(array('status' => 8));
						$grouplogLogic -> where(array('groupid' => $value['groupid'])) -> save(array('isexpired' => 1));
						$groupLogic -> where(array('id' => $value['groupid'])) -> save(array('isexpired' => 1, 'status' => 6));
						//查找开团数
						$product = $productLogic -> where(array('id' => $group['productid'])) -> field('buynumber') -> find();
						//团过期，开团数减1
						$successnumber = $product['buynumber'] - 1;
						$productLogic -> where(array('id' => $group['productid'])) -> save(array('buynumber' => $successnumber));
					}
				}
			}
			//
			foreach($grouplogList as $key => $value){
				$group = $groupLogic -> where(array('id' => $value['groupid'])) -> find();
				$grouplogList[$key]['createtime'] = date('Y-m-d H:i:s', $value['createtime']);
				$grouplogList[$key]['groupid'] = $group['id'];
				$grouplogList[$key]['groupstatus'] = $group['status'];
			}
			if(!empty($grouplogList)){
				$this -> ajaxReturn(array('ret' => 1, 'msg' => $grouplogList));
			}else{
				$this -> ajaxReturn(array('ret' => 2));
			}
			
		}
		$this -> display();
	}
	
	/**
	 * 我的成团详情
	 * @date: 2015-11-10
	 * @author: 陈雄华
	 * @return:
	 */
	public function groupdetail(){
		$grouplogLogic = D('FightgroupGrouplog', 'Logic');
		$productLogic = D('FightgroupProduct', 'Logic');
		$ordeeLogic = D('FightgroupOrder', 'Logic');
		$groupLogic = D('FightgroupGroup', 'Logic');
		//用户信息
		$userinfo = session('userinfo');
		$openid = $userinfo['openid'];
		$nickname = $userinfo['nickname'];
		$id = I('get.id', 0);//团id
		
		//查找谁是团长
		$grouplog = $grouplogLogic -> where(array('groupid' => $id, 'isleader' => 1)) -> find();
		//查出购买商品的内容
		$product = $productLogic -> where(array('id' => $grouplog['productid'])) -> find();
		$grouplog['oriprice'] = $product['oriprice'];//商品市场价
		
		$this -> assign('grouplog', $grouplog);
		//查该团信息
		$group = $groupLogic -> where(array('id' => $id)) -> find();
		$group['wanting'] = $group['groupnumber'] - $group['hasjoin'];//还差多少人成团
		$this -> assign('group', $group);
		
// 		echo $group['groupnumber'];
		//查找团员头像
		$headimg = array();
		$grouplogheadimg = $grouplogLogic -> where(array('groupid' => $id, 'isleader' => 0, 'ishidden' => 0)) -> field('headimgurl') -> select();
// 		dump($grouplogheadimg);
		$j = 0;//比较变量
		for($i = 0; $i < $group['groupnumber']; $i++){
			if($i == 0){
				$headimg[0]['imgurl'] = $grouplog['headimgurl'];//团长头像
			}else{
				if($grouplogheadimg == ''){//如果团员头像为空则赋值null
					$headimg[$i]['imgurl'] = null;
				}else{
					if($j < count($grouplogheadimg)){
						$headimg[$i]['imgurl'] = $grouplogheadimg[$j]['headimgurl'];
					}else{
						$headimg[$i]['imgurl'] = null;
					}
					$j++;
				}
				
			}
			
		}
// 		dump($headimg);
		$this -> assign('headimg', $headimg);
		
		//分享
		$title = $product['name'].$group['groupnumber'].'人团';
		$desc = '成团价：'.$grouplog['productprice'].$product['description'];
		
 		//$url = get_url();
		//$url = str_replace('.html', '', $url);
		$url = C('WEB_ABSOLUTE_URL').'/wechat.php?s=/Mall/productDetail/id/'.$product['id'].'/g/'.$id.'/u/'.WID;
		$share = array(
				'title'	=> $title,
				'desc'	=> $desc,
				'imgpath'=> $product['coverimg'],
				'url'	 => $url,
		
		);
		$this->assign('share', $share);
		
		$this -> display();
	}
	
	/**
	 * 退款申请
	 * @date: 2015-11-6
	 * @author: 陈雄华
	 * @return:
	 */
	public function refundreason(){
		$refundLogic = D('FightgroupRefund', 'Logic');
		$orderLogic = D('FightgroupOrder', 'Logic');
		$productLogic = D('FightgroupProduct', 'Logic');
		
		$orderid=I('get.id',0);//订单id
		if(IS_POST){
			//用户信息
			$userinfo = session('userinfo');
			$openid = $userinfo['openid'];
			$nickname = $userinfo['nickname'];
			
			$where = array();
			$type = I('post.type');//退款方式
			$pay = I('post.pay');//支付宝账号
			$bank = I('post.bank');//银行卡号
			
			//查询退款订单的信息
			$orderid = I('post.orderid');
			$order = $orderLogic -> where(array('id' => $orderid)) -> find();
			//查找商品名
			$product = $productLogic -> where(array('id' => $order['productid'])) -> field('name') -> find();
			
			//判断电话号码格式是否正确，错误则返回提示信息
			$linktel = I('post.linktel');
			if(preg_match("/1[3458]{1}\d{9}$/", $linktel)){
				$linktel = $linktel;
			}else{
				$this->ajaxReturn ( array (
						'ret' => 2,
						'msg'=>'手机号码格式不正确',
				) );
			}
			
			if($type == 1 && $pay != ''){
				$where=array(//支付宝退款
						'uid' => WID,
						'openid' => $openid,
						'nickname' => $nickname,
						'reason' => I('post.reason'),
						'linkman' => I('post.linkman'),
						'linktel' => I('post.linktel'),
						'type' => $type,
						'cardnumber' => $pay,
						'ordernumber' => $order['ordernumber'],//订单号
						'productid' => $order['productid'],//商品id
						'productname' => $product['name'],//商品名
						'productnumber' => $order['buynumber'],//购买数量
						'productprice' => $order['price'],//购买单价
						'totalprice' => $order['totalprice'],//总价
						'groupid' => $order['groupid'],//团id
						'groupnumber' => $order['productgroupnumber'],//团类型人数
						'createtime' => time(),
				);
			}else if($type == 2 && $bank != ''){
				$where=array(//银行卡号退款
						'uid' => WID,
						'openid' => $openid,
						'nickname' => $nickname,
						'reason' => I('post.reason'),
						'linkman' => I('post.linkman'),
						'linktel' => I('post.linktel'),
						'type' => $type,
						'cardnumber' => $bank,
						'ordernumber' => $order['ordernumber'],//订单号
						'productid' => $order['productid'],//商品id
						'productname' => $product['name'],//商品名
						'productnumber' => $order['buynumber'],//购买数量
						'productprice' => $order['price'],//购买单价
						'totalprice' => $order['totalprice'],//总价
						'groupid' => $order['groupid'],//团id
						'groupnumber' => $order['productgroupnumber'],//团类型人数
						'createtime' => time(),
				);
			}else{
				$this -> ajaxReturn(array('ret' => 2, 'msg' => '请填写退款账号'));
			}
				
			$res=$refundLogic -> add($where);
			if($res){
				$orderLogic -> where(array('id' => $orderid)) -> save(array('status' => 6));
				$this -> ajaxReturn(array('ret' => 1, 'msg' => '申请提交成功'));
			}else{
				$this -> ajaxReturn(array('ret' => 2, 'msg' => '申请提交失败'));
			}
			exit;
		}
		$this -> assign('orderid', $orderid);
		$this -> display();
	}
	
	/**
	 * 订单列表
	 * @date: 2015-11-9
	 * @author: 陈雄华
	 * @return:
	 */
	public function orderlist(){
		$orderLogic = D('FightgroupOrder', 'Logic');
		$productLogic = D('FightgroupProduct', 'Logic');
		
		//用户信息
		$userinfo = session('userinfo');
		$openid = $userinfo['openid'];
		$nickname = $userinfo['nickname'];
		
		$where=array(
				'uid' => WID,
				'openid' => $openid,
		);
		if(IS_POST){
			$sign = I('post.sign');
			
			switch($sign){
				case 1:$where['ispay'] = array('eq', 1);$where['status'] = array(array('eq', 2), array('eq', 5), 'or');
				break;
				case 2:$where['status'] = array('eq', 3);
				break;
				case 3:$where['status'] = array('eq', 4);
				break;
				case 4:$where['status'] = array('eq', 8);
				break;
				case 5:$where['status'] = array('eq', 7);
				break;
				case 6:$where['status'] = array('eq', 6);
				break;
			}
			
			
			$count = $orderLogic -> where($where) -> count();
			$page = new Page($count, 10);
			$totalPages = $page-> totalPages;
			$orderList = $orderLogic -> where($where) -> order('createtime desc') -> limit($page -> firstRow,$page -> listRows) -> select();
			
			if($orderList != ''){
				foreach($orderList as $key => $value){
					$orderList[$key]['createtime'] = date('Y-m-d H:i', $value['createtime']);
					$product = $productLogic -> where(array('id' => $value['productid'])) -> field('name') -> find();
					$orderList[$key]['productname'] = $product['name'];
				}
				$this -> ajaxReturn(array('ret' => 1, 'msg' => $orderList));
			}else{
				$this -> ajaxReturn(array('ret' => 2));
			}
			
		}

		$this->display();
	}
	
	/**
	 * 订单列表
	 * @date: 2015-11-9
	 * @author: 陈雄华
	 * @return:
	 */
	public function orderdetail(){
		$orderLogic = D('FightgroupOrder', 'Logic');
		$addressLogic = D('FightgroupAddress', 'Logic');
		$productLogic = D('FightgroupProduct', 'Logic');
		
		$id = I('get.id',0);
		//查找对应id的订单
		$order = $orderLogic -> where(array('id' => $id)) ->find();
		$order['createtime'] = date('Y-m-d H:i', $order['createtime']);
		//通过商品id查询商品名称
		$product = $productLogic -> where(array('id' => $order['productid'])) -> field('name') -> find();
		$order['productname'] = $product['name'];
		
		//查找收货人
		$address = $addressLogic -> where(array('id' => $order['addressid'])) -> find();
		$this -> assign('address', $address);
		
		$this -> assign('order', $order);
		$this -> display();
	}
	
	
	/**
	 * 确认收货
	 * @date: 2015-11-10
	 * @author: 陈雄华
	 * @return:
	 */
	public function shopconfirm(){
		$orderLogic = D('FightgroupOrder', 'Logic');
		$groupLogic = D('FightgroupGroup', 'Logic');
 		
		$id = I('get.id',0);
		$res = $orderLogic -> where(array('id' => $id)) -> save(array('status' => 4));
		if($res){
			//查看该团成团是否全部确认收货
			$order = $orderLogic -> where(array('id' => $id)) -> field('groupid') -> find();
			//查找该团所有订单状态(不包括已退团的)
			$where['groupid'] = array('eq', $order['groupid']);
			$where['status'] = array('neq', 7);
			$orderList = $orderLogic -> where($where) -> field('status') -> select();
			$bool = 0;
			foreach($orderList as $value){
				$sign = '';
				if($value['status'] == 4){
					$sign = 0;
				}else{
					$sign = 1;
				}
				$bool += $sign;
			}
// 			echo $bool;
// 			该团所有订单处于完成状态，则该团也属于完成状态
			if($bool == 0){
				$groupLogic -> where(array('id' => $order['groupid'])) -> save(array('status' => 4));
			}
			redirect(U('orderlist'));
		}else{
			$this -> error('操作失败');
		}
	}
	
	/**
	 * 团过期处理
	 * @date: 2015-11-12
	 * @author: 陈雄华
	 * @return:
	 */
	public function teamoverdue(){
		$orderLogic = D('FightgroupOrder', 'Logic');
		$groupLogic = D('FightgroupGroup', 'Logic');
		$grouplogLogic = D('FightgroupGrouplog', 'Logic');
		$productLogic = D('FightgroupProduct', 'Logic');
		$id = I('post.groupid');//团id
	
		$orderLogic -> where(array('groupid' => $id)) -> save(array('status' => 8));
		$grouplogLogic -> where(array('groupid' => $id)) -> save(array('isexpired' => 1));
		$groupLogic -> where(array('id' => $id)) -> save(array('isexpired' => 1, 'status' => 6));
		//通过团id查找商品id
		$group = $groupLogic -> where(array('id' => $id)) -> field('productid') -> find();
		//查找该团该商品的开团数
		$product = $productLogic -> where(array('id' => $group['productid'])) -> field('buynumber') -> find();
		//团过期，开团数减1
		$successnumber = $product['buynumber'] - 1;
		$productLogic -> where(array('id' => $group['productid'])) -> save(array('buynumber' => $successnumber));
		$this->ajaxReturn(array('ret' => 1));
	}
	
	
	
}















